package com.example.webpractice.controller;


import com.example.webpractice.annotation.IgnoreAuth;
import com.example.webpractice.dto.LoginDTO;
import com.example.webpractice.dto.UpdatePwdDTO;
import com.example.webpractice.dto.UserDTO;
import com.example.webpractice.po.UserPO;
import com.example.webpractice.service.TokenService;
import com.example.webpractice.service.UserService;
import com.example.webpractice.utils.ResponseEnum;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.example.webpractice.utils.ResponseEnum.accountFound;


/**
 * 登录相关
 */
@RequestMapping("users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录
     */
    @IgnoreAuth
    @PostMapping(value = "/login")
    public ServerResponseEntity login(@RequestBody LoginDTO loginDTO) {
        UserPO user = userService.selectByUsername(loginDTO.getUsername());
        if (user == null || !user.getPassword().equals(loginDTO.getPassword())) {
            return ServerResponseEntity.fail(ResponseEnum.loginFail);
        }
        String token = tokenService.generateToken(user.getId(), loginDTO.getUsername(), "users", user.getRole());
        return ServerResponseEntity.success(token);
    }

    /**
     * 密码修改
     */
    @RequestMapping(value = "/updatePasswd")
    public ServerResponseEntity resetPass(@RequestBody UpdatePwdDTO updatePwdDTO) {
        UserPO userPo = userService.selectByUsername(updatePwdDTO.getUsername());
        if (userPo == null) {
            return ServerResponseEntity.fail(ResponseEnum.accountNotFound);
        }
        if (!userPo.getPassword().equals(updatePwdDTO.getOldpasswd())) {
            return ServerResponseEntity.fail(ResponseEnum.loginFail);
        }
        userPo.setPassword(updatePwdDTO.getNewpasswd());
        if (userService.updateUser(userPo) != 1) {
            return ServerResponseEntity.fail(-1, "更新密码失败");
        }
        return ServerResponseEntity.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody UserDTO userDto) {
        UserPO u = userService.selectByUsername(userDto.getUsername());
        if (u != null && u.getId() != userDto.getId() && u.getUsername().equals(userDto.getUsername())) {
            return ServerResponseEntity.fail(accountFound);
        }
        u = new UserPO();
        u.setId(userDto.getId());
        u.setUsername(userDto.getUsername());
        userService.updateUser(u);
        return ServerResponseEntity.success();
    }

    /**
     * 退出
     */
    @GetMapping(value = "logout")
    public ServerResponseEntity logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return ServerResponseEntity.success(0, "退出成功");
    }

    /**
     * 获取用户的session用户信息
     */
    @RequestMapping("/session")
    public ServerResponseEntity getCurrUser(HttpServletRequest request) {
        Long id = (Long) request.getAttribute("userId");
        UserVO user = userService.selectById(id);
        return ServerResponseEntity.success(user);
    }

}
